(function (_super) {
    var io = ppt.io;
    var ACTIONS = {
        "game::begin" : function () {
            this.start();
        },
        "game::result" : function () {
            this.stop();
        }
    };

    function Timer(mode, time) {
        Timer.super(this);

        this.icon = null;
        this.bg = null;
        this.text = null;

        this.minutes = 0;
        this.seconds = 0;

        this.init(mode, time);
    }
    Laya.class(Timer, "Game.Timer", _super);
    var _proto = Timer.prototype;
    _proto.destroy = function () {
        _super.prototype.destroy.call(this, true);

        io.unregister(ACTIONS);

        this.icon = null;
        this.bg = null;
        this.text = null;
        this.minutes = null;
        this.seconds = null;
    }

    _proto.init = function (mode, time) {
        io.register(ACTIONS, this);
        
        this.setup(mode, time);
        this.create();
        this.loop();
    }
    _proto.setup = function (mode, time) {
        this.x = mode == "pve" ? 1055 : 1130;
        this.y = 27;
        
        this.minutes = time / 60 | 0;
        this.seconds = time % 60;
    }
    _proto.create = function () {
        var bg = new Laya.Image("res/game/ui/timer_bg.png");
            bg.alpha = 0.5;

        var text = new Laya.Label();
            text.fontSize = 26;
            text.font = "Arial";
            text.color = "#ffffff";
            text.pos(50, 10);

        var icon = new Laya.Image("res/game/ui/timer_icon.png");
            icon.pos(-28, -7);

        this.bg = bg;
        this.text = text;
        this.icon = icon;
        this.addChildren(bg, text, icon);
    }
    _proto.loop = function () {
        this.seconds --;
        if(this.seconds < 0){
            this.seconds = 59;
            this.minutes --;
        }
        if(this.minutes <= 0 && this.seconds == 0){
            this.stop();
        }

        this.text.text = (this.minutes < 10 ? "0" + this.minutes : this.minutes) + ":" + (this.seconds < 10 ? "0" + this.seconds : this.seconds);
    }

    _proto.start = function () {
        Laya.timer.loop(1000, this, this.loop);
    }
    _proto.stop = function () {
        Laya.timer.clear(this, this.loop);
    }
})(Laya.Sprite);

(function (_super) {
    var io = ppt.io;

    function RebornTimer() {
        RebornTimer.super(this);

        this.timerMask = null;
        this.timer = null;
        this.visible = false;

        this.init();
    }
    Laya.class(RebornTimer, "Game.RebornTimer", _super);
    var _proto = RebornTimer.prototype;
    _proto.destroy = function () {
        _super.prototype.destroy.call(this, true);

        io.unregister("role::time", this.start);

        this.timerMask = null;
        this.timer = null;
    }

    _proto.init = function () {
        io.register("role::time", this, this.start);

        this.setup();
        this.create();
    }
    _proto.setup = function () {
        this.size(1334, Laya.stage.height);
        this.centerX = 0;
        this.centerY = 0;
    }
    _proto.create = function () {
        var timerMask = new Laya.Image("res/public/mask.png");
            timerMask.size(GAME_CONFIG.WIDTH, GAME_CONFIG.WIDTH);
            timerMask.mouseEnabled = true;
            timerMask.alpha = 0.4;
            timerMask.centerX = 0;
            timerMask.centerY = 0;

        var timer = new Laya.Label();
            timer.font = "font_match_reborn";
            timer.align = "center";
            timer.size(500, 70);
            timer.centerX = 0;
            timer.centerY = 0;
            
        this.timerMask = timerMask;
        this.timer = timer;
        this.addChildren(timerMask, timer);
    }
    _proto.loop = function () {
        this.totalTime--;
        this.timer.text = this.totalTime + "s.";
        if(this.totalTime < 0){
            Laya.timer.clearAll(this);
            this.visible = false;
            this.timer.text = "";
        }
    }
    _proto.start = function (data) {
        this.visible = true;
        this.totalTime = data.time;
        this.loop();
        Laya.timer.loop(1000, this, this.loop);
    }
    _proto.resize = function (height) {
        this.centerY = 0;
    }
})(Laya.Sprite);